*******************************************************************************
* main_panel_summary.dta generated from other data sets                       *
* in the replication materials                                                *
*******************************************************************************
clear
use contacts.dta
keep if lob_lastname != ""
keep if congress>=110 & congress<=111
drop lob_lastname1-lob_firstname4
gen year = year(contactdate)
replace year = fyear if year == . & fmonth>3
replace year = fyear-1 if year ==. & fmonth<=3
label var year "year of the contact"
// (1) Contacts
gen c = 1
bys registrantid lob_firstname lob_lastname icpsr year: egen numconts = sum(c)
label var numconts "number of contacts to the politician"
replace c = 0 if contactlevel == "Staff"
bys registrantid lob_firstname lob_lastname icpsr year: egen numdconts = sum(c)
label var numdconts "number of direct contacts to the politician"
drop c
bys registrantid lob_firstname lob_lastname icpsr year clientcountry: gen c = _n == 1
bys registrantid lob_firstname lob_lastname icpsr year: egen ncclients = sum(c)
label var ncclients "number of clients with contacts to the politician"
drop c
gen direct = 0
replace direct = 1 if contactlevel == "Member"
bys registrantid lob_firstname lob_lastname icpsr year clientcountry: egen dd = max(direct)
bys registrantid lob_firstname lob_lastname icpsr year clientcountry: gen c = _n == 1
replace c = 0 if dd == 0
bys registrantid lob_firstname lob_lastname icpsr year: egen ndclients = sum(c)
label var ndclients "number of clients with direct contacts to the politician"
keep registrantid lob_firstname lob_lastname icpsr year congress numconts numdconts ncclients* ndclients*
duplicates drop
save main_panel_summary.dta, replace
// (2) Politician attributes, yearly
clear
use masterdata_congress.dta
keep if congress>=110 & congress<=111
save poltemp.dta, replace
gen year = .
replace year = 2007 if congress == 110
replace year = 2009 if congress == 111
append using poltemp.dta
replace year = 2008 if congress == 110 & year ==.
replace year = 2010 if congress == 111 & year ==.
save poltemp.dta, replace
// (3) Firm attributes
clear
use contacts.dta
merge n:1 registrantid fyear fmonth using registrant_info.dta
drop _merge
gen year = year(contactdate)
replace year = fyear if year == . & fmonth>3
replace year = fyear-1 if year ==. & fmonth<=3
label var year "year of the contact"
collapse (min) startyear (mean) numlob numlob_all (max) domestic ntclients nhclients, by(registrantid year)
label var year "year"
label var startyear "registrant registration year (min)"
label var domestic "LDA listed (max)"
label var numlob "number of lobbyists (max)"
label var numlob_all "number of lobbyists, ever appeared (max)"
label var ntclients "number of total foreign clients (max)"
label var nhclients "number of total foreign clients, 5 years (max)"
save regtemp.dta, replace
// (4) Create all possible combinations
clear
use main_panel_summary.dta
keep registrantid lob_lastname lob_firstname year 
duplicates drop
joinby year using poltemp.dta
erase poltemp.dta
merge n:1 registrantid lob_lastname lob_firstname year icpsr using main_panel_summary.dta
replace numconts = 0 if _merge == 1
replace numdconts = 0 if _merge == 1
replace ncclients = 0 if _merge == 1
replace ndclients = 0 if _merge == 1
order registrantid lob_lastname lob_firstname icpsr lastname year numconts numdconts ncclients ndclients
drop _merge
merge n:1 registrantid year using regtemp.dta
drop if _merge == 2
drop _merge
merge n:1 registrantid lob_lastname lob_firstname using lobbyist_attributes.dta
drop if _merge == 2
drop _merge
save main_panel_summary.dta, replace
erase regtemp.dta
// (5) Summarize the data
clear
use main_panel_summary.dta
// ideology difference
gen cfdiff = abs(cfscore-polcfscore)
label var cfdiff "CF score difference btw politician and lobbyist"
gen lob_dwmean = .
replace lob_dwmean = expol_dw if expol_dw != .
replace lob_dwmean = mem1_dw if mem1_dw! = . & mem2_dw == .
replace lob_dwmean = (mem1_dw+mem2_dw)/2 if mem1_dw! = . & mem2_dw != . & mem3_dw == . 
replace lob_dwmean = (mem1_dw+mem2_dw+mem3_dw)/3 if mem1_dw! = . & mem2_dw != . & mem3_dw != . 
gen dwdiff = abs(dwnom1-lob_dwmean)
label var dwdiff "DWNOMINATE score difference btw politician and lobbyist"
gen partydiff = .
replace partydiff = 1 if democrat == 1 & lob_rep == 1
replace partydiff = 1 if democrat == 0 & lob_rep == 0
replace partydiff = 0 if democrat == 0 & lob_rep == 1
replace partydiff = 0 if democrat == 1 & lob_rep == 0
label var partydiff "Party difference btw politician and lobbyist"
// contacts
bys registrantid lob_firstname lob_lastname icpsr: egen any = max(ncclients)
replace any = 1 if any>0
label var any "any contacts during the period of study"
bys registrantid lob_firstname lob_lastname icpsr: egen dany = max(ndclients)
replace dany = 1 if dany>0
label var dany "any direct contacts during the period of study"
// selectivity
gen select_cur = ncclients/ntclients
label var select_cur ""
gen dselect_cur = ndclients/ntclients
label var dselect_cur ""
// politician attributes
replace primary_committee = upper(primary_committee)
gen leader = 0
replace leader = 1 if maj_leader == 1
replace leader = 1 if min_leader == 1
replace leader = 1 if primary_committee == "SPEAKER"
replace leader = 1 if primary_committee == "MAJORITY LEADER"
replace leader = 1 if primary_committee == "MAJORITY WHIP"
replace leader = 1 if primary_committee == "MINORITY LEADER"
replace leader = 1 if primary_committee == "MINORITY WHIP"
gen foreign = 0
replace foreign = 1 if primary_committee == "FOREIGN AFFAIRS"
replace foreign = 1 if primary_committee == "FOREIGN RELATIONS"
gen tight = 0
replace tight = 1 if votepct <55
bys icpsr: egen majority_max = max(majority)
bys icpsr: egen dem_max = max(democrat)
bys icpsr: egen house_max = max(house)
bys icpsr: egen chair_max = max(chair)
bys icpsr: egen leader_max = max(leader)
bys icpsr: egen foreign_max = max(foreign)
bys icpsr: egen seniority_min = min(seniority)
bys icpsr: egen fresh_min = min(fresh)
bys icpsr: egen tight_max = min(tight)
// other characteristics
gen numlob2 = numlob*numlob
gen nhclients2 = nhclients*nhclients
replace expol = 0 if expol == .
// member-lobbyist pairs
macro define lob_char "numlob numlob2 startyear nhclients nhclients2 domestic expol staffer whitehouse"
macro define mem_char "majority_max dem_max house_max chair_max leader_max foreign_max seniority_min fresh_min tight_max"
collapse (max) any dany (mean) cfdiff dwdiff partydiff $lob_char $mem_char select_cur dselect_cur numconts numdconts ncclients ndclients, by(registrantid lob_firstname lob_lastname icpsr)
label var any "=1 if there was any lobbying contact btw the pair"
label var dany "=1 if there was any direct lobbying contact btw the pair" 
label var cfdiff "CF score difference btw the pair" 
label var dwdiff "DW NOMINATE score difference (if lobbyist had Congressional experience)"
label var partydiff "party difference btw the pair"
label var numlob "number of lobbyists in the lobbying firm" 
label var numlob2 "numlob squared"
label var startyear "FARA registration year of the lobbying firm"
label var nhclients "number of total foreign clients, 5 years"
label var nhclients2 "nhclients squared"
label var domestic "LDA registered"
label var expol "=1 if lobbyist was a House Represenatitive, =2 if lobbyist was a Senator"
label var staffer "=1 if ex-staffer lobbyist"
label var whitehouse "=1 if lobbyist worked in the executive branch of the gov" 
label var majority_max "=1 if member in the majority party, max over the period"
label var dem_max "=1 if Democrat, max over the period"
label var house_max "=1 if House representative, max over the period"
label var chair_max "=1 if chair in a committee, max over the period"
label var leader_max "=1 if in the party leadership, max over the period"
label var foreign_max "=1 if in foreign affairs/relations committees, max over period"
label var seniority_min "seniority"
label var fresh_min "=1 if elected for the first time during the period of study"
label var tight_max "=1 if previous vote share <55%"
label var select_cur "frac of current clients with no contacts to the politician" 
label var dselect_cur "frac of current clients with no direct contacts to the politician"
label var numconts "average number of contacts per year btw the pair" 
label var numdconts "average number of direct contacts per year btw the pair" 
label var ncclients "average number of clients with any contect per year"
label var ndclients "average number of clients with any direct contact per year"
save main_panel_summary.dta, replace
